var app = getApp();
var bmap = require('../../libs/bmap-wx.min.js');
var wxMarkerData = []; 
Page({
  data: {
            apis: app.data.ipconfig,
        indicatorDots: true,
        autoplay: true,
        interval: 2000,
        duration: 500,
        circular: true,
        loading: '',
    markers: [],
    latitude: '',
    longitude: '',
    address: ''
  },
  makertap: function (e) {
    var that = this;
    var id = e.markerId;
    that.showSearchInfo(wxMarkerData, id);
  },
  onLoad: function () {
    var that = this;
    // 新建百度地图对象 
    var BMap = new bmap.BMapWX({
      ak: 'zFIjZTRG9GZgnYXXwb8gSB3wOQljt6zM'
    });
    var fail = function (data) {
      console.log(data)
    };
    var success = function (data) {
      wxMarkerData = data.wxMarkerData;
      that.setData({
        markers: wxMarkerData
        
      });
      that.setData({
        address: wxMarkerData[0].address
      });
      that.setData({
        latitude: wxMarkerData[0].latitude
      });
      that.setData({
        longitude: wxMarkerData[0].longitude
      });
    }
    // 发起regeocoding检索请求 
    BMap.regeocoding({
      fail: fail,
      success: success,
      
    });
            //查询轮播图片进行渲染显示
        wx.request({
            url: that.data.apis + '/wxxiaochengxu/homebannerApi/homebannerList',
            data: {appid: app.data.appid, openid: wx.getStorageSync('openid')},
            header: {
                'content-type': 'application/json'
            },
            success: function (res) {
                console.log(res);
                that.setData({
                    swiperBanner: res.data.res.homebannerlist,
                    loading: 'hidden'
                })
            },
            fail: function () {
                console.log("接口出现异常");
            }
        })
        //查询首页分类进行渲染显示
        wx.request({
            url: that.data.apis + '/wxxiaochengxu/homebannerApi/getmenus',
            data: {appid: app.data.appid, openid: wx.getStorageSync('openid')},
            header: {
                'content-type': 'application/json'
            },
            success: function (res) {
                that.setData({
                    datalist: res.data.res
                })
            },
            fail: function () {
                console.log("接口出现异常");
            }
        })
        //查询今日首推进行渲染显示
        wx.request({
            url: that.data.apis + '/wxxiaochengxu/homebannerApi/getDevaluationList',
            data: {appid: app.data.appid, openid: wx.getStorageSync('openid')},
            header: {
                'content-type': 'application/json'
            },
            success: function (res) {
                //console.log(res.data.res)
                that.setData({
                    todayShops: res.data.res
                })
            },
            fail: function () {
                console.log("接口出现异常");
            }
        })
  },
  showSearchInfo: function (data, i) {
    var that = this;
    console.log(data)
   
  },
      //点击轮播图片进行跳转
    thisClick: function (e) {
        if (e.currentTarget.dataset.btype == 1) {
            console.log('跳转多商品');
            wx.navigateTo({
                url: '../../pages/more/index?flag=1&id=' + e.currentTarget.dataset.homebannerid+'&head=0&menusname='
            })
        } else if (e.currentTarget.dataset.btype == 2) {
            console.log('跳转单商品');
            wx.navigateTo({
                url: '../../pages/shopDetails/index?shopid=' + e.currentTarget.dataset.bcontent
            })
        } else if (e.currentTarget.dataset.btype == 3) {
            console.log('跳转宣传页');
            wx.navigateTo({
                url: '../../pages/publicity/index?flag=1&id='+e.currentTarget.dataset.homebannerid
            })
        }
    }
    ,
    ///选择位置
      chooseLocation: function (e) {
      var that = this
      wx.chooseLocation({
        success: function (res) {
          // success
          console.log(res);
          that.setData({
            longitude: res.longitude,
            latitude: res.latitude,
            address: res.address
          })
        },
        fail: function () {
          // fail
        },
        complete: function () {
          // complete
        }
      })
    },
        skipPage: function (e) {
        var menuid = e.currentTarget.dataset.menuid;
        var id = e.currentTarget.dataset.id;
        var menusname = e.currentTarget.dataset.menusname;
        wx.navigateTo({
          url: '../../pages/typeDetail/typeDetail' 
        })
        // if (menuid == 1) {
        //     console.log('商品列表');
        //     wx.navigateTo({
        //         url: '../../pages/typeall/index?flag=2&id=' + id+'&head=0&menusname='+menusname
        //     })
        // } else if (menuid == 2) {
        //     console.log('展示页面');
        //     wx.navigateTo({
        //         url: '../../pages/publicity/index?flag=2&id='+id
        //     })
        // } else if (menuid == 3) {
        //     console.log('优惠券');
        //     wx.navigateTo({
        //         url: '../../pages/newticket/index'
        //     })
        // }
    }
    , shopDetail: function (e) {
        var shopid = e.currentTarget.dataset.shopid;
        wx.navigateTo({
            url: '../../pages/shopDetails/index?shopid=' + shopid
        })
    }

})


